package com.kuang.volatileTest;

import java.util.concurrent.atomic.AtomicInteger;

public class demo {
    private  volatile static AtomicInteger sum=new AtomicInteger(0);

    //加法
    public static void add(){
        sum.getAndAdd(3);
    }

    public static void main(String[] args) {
        for(int i=1;i<=20;i++){
            new Thread(()->{
                for(int j=1;j<=1000;j++){
                    add();
                }
            },String.valueOf(i)).start();
        }
        while (Thread.activeCount()>2){ // main gc
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + " " + sum);
    }
}
